package photostock.view;

public class FormWidget extends Widget {
	
	private static final long serialVersionUID = 20090501L;
	private String id;
	private String action;
	
	public FormWidget(String id, String action) {
		super("form", "form", id+"Form");
		setAttribute("method", "POST");
		setAttribute("onsubmit", "return false;");

		this.id = id;
		this.action = action;
		Widget img = new Widget("img", "formImg", id+"Img");
		img.setAttribute("src", "");
		Widget fieldSet = new Widget("fieldset", "formFieldset", id+"Fieldset");
		Widget buttons = new Widget("div", "formButtons", id+"Buttons");
		addContent(img);
		addContent(fieldSet);
		addContent(buttons);
	}
	
	public void addText(String name, String script) {
		Widget text = new Widget("input", "text", name);
		text.setAttribute("name", name);
		text.setAttribute("type", "text");
		text.setAttribute("value", "");
		if (!script.equals("")) text.setAttribute("onblur", script);
		getElement(id+"Fieldset").addContent(text);
		getElement(id+"Fieldset").addContent(new Widget("br"));
		getElement(id+"Fieldset").addContent(new Widget("br"));
	}
	
	public void addPassword(String name, String script) {
		Widget password = new Widget("input", "password", name);
		password.setAttribute("name", name);
		password.setAttribute("type", "password");
		password.setAttribute("value", "");
		if (!script.equals("")) password.setAttribute("onblur", script);
		getElement(id+"Fieldset").addContent(password);
		getElement(id+"Fieldset").addContent(new Widget("br"));
		getElement(id+"Fieldset").addContent(new Widget("br"));
	}

	public void addCheckbox(String value, String script) {
		Widget check = new Widget("input");
		check.setAttribute("type", "checkbox");
		check.setAttribute("value", value);
		if (!script.equals("")) check.setAttribute("onclick", script);
		getElement(id+"Fieldset").addContent(check);
		getElement(id+"Fieldset").addContent(new Widget("br"));
		getElement(id+"Fieldset").addContent(new Widget("br"));
	}


	public void addValidButton(String nom) {
		Widget button = new Widget("input", "button", id+"ValidButton");
		button.setAttribute("name", nom);
		button.setAttribute("type", "button");
		button.setAttribute("value", nom);
		button.setAttribute("onclick", action);
		getElement(id+"Buttons").addContent(button);
	}

	public void addButton(String nom, String script) {
		Widget button = new Widget("input", "button", id+nom+"Button");
		button.setAttribute("name", nom);
		button.setAttribute("type", "button");
		button.setAttribute("value", nom);
		button.setAttribute("onclick", script);
		getElement(id+"Buttons").addContent(button);
	}

	
	public void addLabel(String s) {
		Widget label = new Widget("label");
		label.setText(s);
		getElement(id+"Fieldset").addContent(label);
	}
	
	public void setImage(String s) {
		getElement(id+"Img").getAttribute("src").setValue(s);
	}
}
